
var appI =getApp()
var globalData = appI.globalData
var bgMusic = appI.globalData.bgMusic
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    activeTabId:0,//当前激活的导航页面
    indexData:[], //首面的数据
    nowMusicId:{},//当前播放的可区信息
    //动态列表的数据
    bodanList:{},
    lishiJiLu:{},
    myBodan:{},
    ani:'',
    activeSwiperChange_list:2,
    zhezhaoHidden:'0',
    jingWen:'智慧人必发光如同天上的光；那使多人归义的，必发光如星，直到永永远远。（但12︰3）',
    playListStatus:0,
    addBoDanComponentVal:"1",
    myBoDanI:{data:[]},
  },
  // 显示动态列表
  showListBoDan:function(){


     var animation = globalData.showListBoDan('show').export()

     this.setData({
      ani: animation,
      zhezhaoHidden:'',
      playListStatus:wx.getStorageSync('playListStatus')
     })

     var dongTaiLieBiao = globalData.getDongTaiLieBiao()

     var nowMusicId = JSON.parse(wx.getStorageSync('nowMusicId'))

      this.setData({
        bodanList:dongTaiLieBiao.bodanList,
        lishiJiLu:dongTaiLieBiao.lishiJiLu,
        myBodan:dongTaiLieBiao.myBodan,
        nowMusicId:nowMusicId,
      })

    
  },
// 隐藏动态列表
  hiddenBoDan:function(){

    var animation = globalData.showListBoDan('hidden').export()

     this.setData({
      ani: animation,
      zhezhaoHidden:'1'
     })

  },
  //通过动态列表播放音乐
  DongTaiPlay:function(e){

    this.setData( globalData.DongTaiPlay(e))

  },
  onPlayListStatus:function(){
    var playListStatus = globalData.onPlayListStatus()
    this.setData({playListStatus:playListStatus})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    //显示当前的目录 
    this.getDbmulu()

    if(wx.getStorageSync('nowMusicId')!==''){

      var nowMusicId = JSON.parse(wx.getStorageSync('nowMusicId'));
      nowMusicId['status']=0;
      wx.setStorageSync('nowMusicId', JSON.stringify(nowMusicId))
      
    }



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

    //实例化组件addBoDan
    this.addBoDan = this.selectComponent("#addBoDan");
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    var self =  this

    var playListStatus = wx.getStorageSync('playListStatus')

    if(typeof playListStatus == 'string'){

      wx.setStorageSync('playListStatus', 0)
      this.setData({playListStatus:0})

    }else{

      this.setData({playListStatus:playListStatus})
    }


    //判断当前播放状态修改播放器的样式
    var nowMu = wx.getStorageSync('nowMusicId')

    if(nowMu !==''){

      this.setData({
        nowMusicId:JSON.parse(nowMu)
      })

      bgMusic.onPlay(function(){
        var nowMusicId = JSON.parse(wx.getStorageSync('nowMusicId'))
        self.setData({
          nowMusicId:nowMusicId
        })
      })

    }

    globalData.onPlay(function(){


      var nowMusicId = wx.getStorageSync('nowMusicId')

      var nowObj = JSON.parse(nowMusicId)

      self.setData({
        nowMusicId:nowObj
      })




    })

    globalData.onPause(function(){
      

      var nowMusicId = wx.getStorageSync('nowMusicId')

      var nowObj = JSON.parse(nowMusicId)

      self.setData({
        nowMusicId:nowObj
      })


    })

    var dongTaiLieBiao = globalData.getDongTaiLieBiao()


    this.setData({
      bodanList:dongTaiLieBiao.bodanList,
      lishiJiLu:dongTaiLieBiao.lishiJiLu,
      myBodan:dongTaiLieBiao.myBodan,
    })


    var myBoDanI = wx.getStorageSync('myBoDan')

    if(myBoDanI !== ''){

      myBoDanI = JSON.parse(myBoDanI)

      this.setData(
        {
          myBoDanI:myBoDanI
        }
      )


    }
    


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  /*页面跳转*/
  tolist:function(e){


    var uid = e.currentTarget.dataset.uid

    var name = e.currentTarget.dataset.name

    wx.navigateTo({
      url: '../list/list'
    })

    var idsStr =  JSON.stringify({uid:uid,tabid:this.data.activeTabId,name:name})

    wx.setStorage({
      data: idsStr,
      key: 'uid'
    })

  },



  //点击Tab块激当前选中样式
  tabActiveStyle:function(e){
    // console.log(e)
    this.setData({
      activeTabId:e.currentTarget.dataset.tabid
    })
  },
  //首页tab和ul数据调取
  getDbmulu:function(){ 

      var self =  this

      wx.cloud.callFunction({
        name:'getDbmulu',
        success:function(res){
         
          self.setData({
            indexData:res.result.data
          })
        },
        fail:function(res){
          
         
          wx.showToast({

            title: '请检查网络连接',
       
            icon:'none',
       
            duration: 2000//持续的时间
       
          })
          

        },
      })

  },
  //清除缓存
  clearStorage:function(){
    wx.clearStorage({
      success: (res) => {
        sonsole.log('清除历史数据')
      },
    })
  },
  //滑块移动事件设置当前激活的Tab样式
  swiperChange:function(e){

    var current = e.detail.current

    // console.log(current)

    this.setData({
      activeTabId:current
    })

  },

  swiperChange_list:function(e){

    var current = e.detail.current

    // console.log(current)

    this.setData({
      activeSwiperChange_list:current
    })

  },
  //  设置底部播放按钮
  botPlayIconSet:function (params) {

    var nowMu = wx.getStorageSync('nowMusicId')

    var self = this;

    if(nowMu == ''){

      wx.showToast({
        title: '当前无播放歌曲',
      })

    }else{


      var nowMuObj = JSON.parse(nowMu)  

      var statur = nowMuObj.status == 1?0:1

      if(statur == 1){

        // 设置播放链接
        globalData.playURLIsOrNo()

        var nowMusicId = JSON.parse(wx.getStorageSync('nowMusicId'))

        nowMusicId['status'] = 1

        wx.setStorageSync('nowMusicId', JSON.stringify(nowMusicId))

        self.setData({nowMusicId:nowMusicId})

        bgMusic.play()

      }else{

        bgMusic.pause()

      }

      nowMuObj['status'] = statur

      this.setData({
        nowMusicId:nowMuObj
      })

      wx.setStorage({
        data: JSON.stringify(nowMuObj),
        key: 'nowMusicId',
      })

    }

  },
  toplay:function(){

    var nowMusicId = wx.getStorageSync('nowMusicId')

    if(nowMusicId == ''){
      return;
    }

    wx.navigateTo({
      url: '../play/play',
    })

  },
  
  addBoDanI:function(){


    this.addBoDan.show();

    // console.log(this.addBoDan.data)

  },
  _add:function(){

    // console.log('触发确定按钮')

    var myBoDanI = wx.getStorageSync('myBoDan')

    if(myBoDanI !== ''){

      myBoDanI = JSON.parse(myBoDanI)

      this.setData(
        {
          myBoDanI:myBoDanI
        }
      )


    }


  },
  //删除自定义播单
  dropBoDan:function(e){

    var uid = e.currentTarget.dataset.uid

    // console.log(uid)

    var myBoDanStr = wx.getStorageSync('myBoDan')

    if(myBoDanStr ==''){

      wx.showToast({
        title: '未发现播单，请刷新页面',
        icon:'none'
      })

      return
    }

    var myBoDan = JSON.parse(wx.getStorageSync('myBoDan'))

    var data = myBoDan.data

    for( var i = 0 ; i　< data.length ; i++){


      if(data[i].id == uid){

        data.splice(i,1)

        break;

      }

    }

    myBoDan.data = data

    wx.setStorageSync('myBoDan', JSON.stringify(myBoDan))

    this.setData({
      myBoDanI:myBoDan
    })

    wx.removeStorageSync(uid)


  }

  
  

})